{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# C++ 调用 C 程序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在 C++ 中引用 C 语言中的函数和变量，在包含 C 语言头文件（假设为 `cfile.h` ）：\n",
    "\n",
    "````{tab-set-code}\n",
    "\n",
    "```{literalinclude} utils/cfile.h\n",
    ":language: c\n",
    "```\n",
    "\n",
    "```{literalinclude} utils/cfile.c\n",
    ":language: c\n",
    "```\n",
    "````"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "c++ 调用 `add`："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "extern \"C\"\n",
    "{\n",
    "    #include \"utils/cfile.h\"\n",
    "}\n",
    "int main(int argc, char* argv[])\n",
    "{\n",
    "    add(2, 3);\n",
    "    return 0;\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "C++14",
   "language": "C++14",
   "name": "xcpp14"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "c++",
   "version": "14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
